import { validatenull } from './Validate.js'
/**
 * 获取字典
 */
export const setDic = (dicData, DIC) => {
  return (typeof (dicData) === 'string') ? DIC : dicData
}
/**
 * 设置px
 */
export const setPx = (val, defval) => {
  if (validatenull(val)) {
    val = defval
  }
  val = val + ''
  if (val.indexOf('%') === -1) {
    val = val + 'px'
  }
  return val
}

export const getTree = (getData, inDAta) => {
  for (let i = 0; i < inDAta.length; i++) {
    getData.push({
      value: inDAta[i].name,
      label: inDAta[i].name
    })
    if (inDAta[i].children.length > 0) {
      getTree(getData, inDAta[i].children)
    }
  }
}

// 递归2,value赋值给id
export const getTree2 = (getData, inDAta) => {
  for (let i = 0; i < inDAta.length; i++) {
    getData.push({
      value: inDAta[i].id,
      label: inDAta[i].name
    })
    if (inDAta[i].children.length > 0) {
      getTree2(getData, inDAta[i].children)
    }
  }
}

/**
 * 设置浏览器头部标题
 */
export const setTitle = function (title) {
  title = title ? `${title}` : '广东智瑞科技开发框架'
  window.document.title = title
}
/**
 * 浏览器判断是否全屏
 */
export const fullscreenToggel = () => {
  if (fullscreenEnable()) {
    exitFullScreen()
  } else {
    reqFullScreen()
  }
}
/**
 * esc监听全屏
 */
export const listenfullscreen = (callback) => {
  function listen () {
    callback()
  }
  document.addEventListener('fullscreenchange', function (e) {
    listen()
  })
  document.addEventListener('mozfullscreenchange', function (e) {
    listen()
  })
  document.addEventListener('webkitfullscreenchange', function (e) {
    listen()
  })
  document.addEventListener('msfullscreenchange', function (e) {
    listen()
  })
}

/**
 * 浏览器判断是否全屏
 */
export const fullscreenEnable = () => {
  let isFullscreen = document.fullscreenEnabled ||
    window.fullScreen ||
    document.mozFullscreenEnabled ||
    document.webkitIsFullScreen
  return isFullscreen
}

/**
 * 浏览器全屏
 */
export const reqFullScreen = () => {
  if (document.documentElement.requestFullScreen) {
    document.documentElement.requestFullScreen()
  } else if (document.documentElement.webkitRequestFullScreen) {
    document.documentElement.webkitRequestFullScreen()
  } else if (document.documentElement.mozRequestFullScreen) {
    document.documentElement.mozRequestFullScreen()
  }
}

/**
 * 浏览器退出全屏
 */
export const exitFullScreen = () => {
  if (document.documentElement.requestFullScreen) {
    document.exitFullScreen()
  } else if (document.documentElement.webkitRequestFullScreen) {
    document.webkitCancelFullScreen()
  } else if (document.documentElement.mozRequestFullScreen) {
    document.mozCancelFullScreen()
  }
}
/**
 * 获取列表高度
 */
export const tableHeight = (size) => {
  // 38 搜索头部 36 tagbox 65 头部 36是搜索条件 33 更多隐藏  20 多余空出来
  let documentHeight = ((window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight) - 200)
  let height = parseInt(size / 3) * 37
  return documentHeight - height - 20
}
/**
 * 获取模态框高度
 * */
export const findParent = (menu, id) => {
  for (let i = 0; i < menu.length; i++) {
    if (menu[i].children.length !== 0) {
      for (let j = 0; j < menu[i].children.length; j++) {
        if (menu[i].children[j].id === id) {
          return menu[i]
        } else {
          if (menu[i].children[j].children.length !== 0) {
            return findParent(menu[i].children[j].children, id)
          }
        }
      }
    }
  }
}
// 整数字转中文大写
export const toChies = (values) => {
  let len = values.length// 统计出长度
  let arr = []
  let chinList = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']// 所有的数值对应的汉字
  let chinLisp = ['仟', '佰', '拾', '亿', '仟', '佰', '拾', '万', '仟', '佰', '拾']// 进制
  for (let i = 0; i < len; i++) {
    arr.push(parseInt(values[i])) // 输入的数据按下标存进去   存进去的只是数字
    arr[i] = chinList[arr[i]] // 是根据我们输入的输入的数字，对应着我们的chin_list这个数组
  }
  for (let i = len - 1, j = 1; i > 0; i--) { // i=2 1//倒序为了添加进制，方便我们去观看
    arr.splice(i, 0, chinLisp[chinLisp.length - j++]) // j=2
  }
  arr = arr.join('')
  return arr
}
// 转中文大写，小数也可以
export const toEChies = (values) => {
  if (values.length === 0) {
    return ''
  }
  let zero = values.indexOf('.')
  let integerNum = ''
  let decimalNum = ''
  if (zero > -1) {
    // 123.45
    values = values.substring(0, zero + 3)
    integerNum = values.substring(0, zero)
    decimalNum = values.substring(zero + 1)
  } else {
    integerNum = values
  }
  let len = integerNum.length// 统计出长度
  let arr = []
  let chinList = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']// 所有的数值对应的汉字
  let chinLisp = ['仟', '佰', '拾', '亿', '仟', '佰', '拾', '万', '仟', '佰', '拾']// 进制
  for (let i = 0; i < len; i++) {
    arr.push(parseInt(integerNum[i])) // 输入的数据按下标存进去   存进去的只是数字
    arr[i] = chinList[arr[i]] // 是根据我们输入的输入的数字，对应着我们的chin_list这个数组
  }
  // 123['壹','佰','贰','拾','叁']
  for (let i = len - 1, j = 1; i > 0; i--) { // i=2 1//倒序为了添加进制，方便我们去观看
    arr.splice(i, 0, chinLisp[chinLisp.length - j++]) // j=2
  }
  arr = arr.join('')
  arr += '元'
  for (let i = 0; i < decimalNum.length; i++) {
    arr += chinList[decimalNum[i]]
    if (i === 0) {
      arr += '角'
    } else if (i === 1) {
      arr += '分'
    }
  }
  if (zero === -1 || zero === values.length - 1) {
    arr += '整'
  }
  return arr
}
/**
 * 总体路由处理器
 */
export const resolveUrlPath = (url, name) => {
  let reqUrl = url
  if (url.indexOf('http') !== -1 || url.indexOf('https') !== -1) {
    reqUrl = `/myiframe/urlPath?src=${reqUrl}&name=${name}`
  } else {
    reqUrl = `${reqUrl}`
  }
  return reqUrl
}
/**
 * 总体路由设置器
 */
export const setUrlPath = ($route) => {
  let value = ''
  if ($route.query.src) {
    value = $route.query.src
  } else {
    value = $route.path
  }
  return value
}
/**
 * 动态插入css
 */
export const uuid = (len, radix) => {
  len = 8
  radix = 5
  let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  let uuid = []
  let i
  radix = radix || chars.length
  if (len) {
    for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]
  } else {
    let r
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
    uuid[14] = '4'
    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | Math.random() * 16
        uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r]
      }
    }
  }
  return uuid.join('')
}

export const loadStyle = url => {
  const link = document.createElement('link')
  link.type = 'text/css'
  link.rel = 'stylesheet'
  link.href = url
  const head = document.getElementsByTagName('head')[0]
  head.appendChild(link)
}
/**
 * 根据字典的value显示label
 */
export const findByvalue = (dic, value) => {
  let result = ''
  if (validatenull(dic)) return value
  if (typeof (value) === 'string' || typeof (value) === 'number' || typeof (value) === 'boolean') {
    let index = 0
    index = findArray(dic, value)
    if (index !== -1) {
      result = dic[index].label
    } else {
      result = value
    }
  } else if (value instanceof Array) {
    result = []
    let index = 0
    value.forEach(ele => {
      index = findArray(dic, ele)
      if (index !== -1) {
        result.push(dic[index].label)
      } else {
        result.push(value)
      }
    })
    result = result.toString()
  }
  return result
}
/**
 * 根据字典的value查找对应的index
 */
export const findArray = (dic, value) => {
  for (let i = 0; i < dic.length; i++) {
    if (dic[i].value === value) {
      return i
    }
  }
  return -1
}
/**
 * 生成随机len位数字
 */
export const randomLenNum = (len, date) => {
  let random = ''
  random = Math.ceil(Math.random() * 100000000000000).toString().substr(0, 4)
  if (date) random = random + Date.now()
  return random
}

export const getWelocom = () => {
  let now = new Date(); let hour = now.getHours()
  if (hour < 6) { return '凌晨好！' } else if (hour < 9) { return '早上好！' } else if (hour < 12) { return '上午好！' } else if (hour < 14) { return '中午好！' } else if (hour < 17) { return '下午好！' } else if (hour < 19) { return '傍晚好！' } else if (hour < 22) { return '晚上好！' } else { return '夜里好！' }
}

export const getcolor = (i) => {
  let concolor = ['#2ec7c9', '#b6a2de', '#5ab1ef', '#ffb980', '#d87a80', '#8d98b3', '#e5cf0d', '#97b552', '#95706d', '#dc69aa', '#07a2a4', '#9a7fd1', '#588dd5', '#f5994e', '#c05050', '#59678c', '#c9ab00', '#7eb00a', '#6f5553', '#c14089', '#9b8bba', '#e098c7', '#8fd3e8', '#71669e', '#cc70af', '#7cb4cc', '#c12e34', '#e6b600', '#0098d9', '#2b821d', '#005eaa', '#339ca8', '#cda819', '#32a487']
  return i < concolor.length ? concolor[(i - 1)] : concolor[(Math.floor(Math.random() * concolor.length))]
}
